package com.xctech.ipb.common.vo;


import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.List;

import static com.xctech.ipb.common.response.ResponseCode.SUCCESS;


/**
 * @author Hollis
 */
@Getter
@Setter
public class Result<T>  implements Serializable {
    /**
     * 状态吗
     */
    private String code;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 消息描述
     */
    private String message;

    /**
     * 链路id
     * */
    private String traceId;

    /**
     * 数据，可以是任何类型的VO
     */
    private T data;

    public Result() {
    }

    public Result(Boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;

    }

    public Result(Boolean success, String code, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.code = code;

    }


    public static <T> Result<T> success(T data) {
        return new Result<>(true, SUCCESS.name(), SUCCESS.name(), data);
    }

    public static <T> Result<T> error(String errorCode,String errorMsg) {
        return new Result<>(false, errorCode, errorMsg, null);
    }


    // 分页数据专用方法
    public static <T> Result<PageData<T>> pageSuccess(int pageNum, int pageSize, long total, List<T> list) {
        Result<PageData<T>> result = new Result<>();
        result.setCode(SUCCESS.name());
        result.setSuccess(true);
        result.setMessage(SUCCESS.name());
        result.traceId = null;
        result.setData(new PageData<>(pageNum, pageSize, total, list));
        return result;
    }
}
